package com.swf.seed.os.command.impl;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;

import com.swf.seed.base.SdCharset;
import com.swf.seed.base.SdLetter;
import com.swf.seed.base.SystemContants;
import com.swf.seed.os.annotation.Command;
import com.swf.seed.security.cert.SecuritySunSigner;

@Command("sign")
public class SignCommand extends CommandAdaptor{

	@Override
	public void option() {
		StringBuilder builder = new StringBuilder();
		builder.append("Description:").append(SystemContants.LINE_SEPARATOR);
		builder.append("  supported algorithm:MD5,SHA-1").append(SystemContants.LINE_SEPARATOR);
		builder.append("Example:").append(SystemContants.LINE_SEPARATOR);
		builder.append("  sign -a MD5 -c GBK -s abcd");
		footer = builder.append(super.footer);
		options.addOption(Option.builder(SdLetter.a).longOpt("algorithm").argName("algorithm" )
				.numberOfArgs(1).desc("algorithm name" ).required().build());  
		options.addOption(Option.builder(SdLetter.c).longOpt("charset").argName("charset" )
				.numberOfArgs(1).desc("charser" ).build());  
		options.addOption(Option.builder(SdLetter.s).longOpt("str").argName("string" )
				.numberOfArgs(1).desc("string to sign" ).required().build()); 
	}

	@Override
	void customizedExecute(CommandLine commandLine) throws Exception {
		String algorithm = commandLine.getOptionValue(SdLetter.a);
		String charset = commandLine.getOptionValue(SdLetter.c);
		if (charset==null){
			charset = SdCharset.defaultCharset;
		}
		String str = commandLine.getOptionValue(SdLetter.s);
		SecuritySunSigner signer = new SecuritySunSigner();
		String encodeStr = signer.encode(str, charset, algorithm);
		System.out.println(encodeStr);
	}
	
}
